package com.xym.manage.controller;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.xym.manage.pojo.ContentCategory;
import com.xym.manage.service.ContentCategoryService;

@Controller
@RequestMapping("content/category")
public class ContentCategoryController {

	@Autowired
	private ContentCategoryService contentCategoryService;

	/**
	 * 删除内容分类节点
	 * @param id
	 * @param parentId
	 * @return void
	 */
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> deleteContentCategory(@RequestParam("id")Long id,@RequestParam("parentId")Long parentId){
		try {
			Boolean flag =contentCategoryService.deleteContentCategory(id,parentId);
			if(flag){
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	
	/**
	 * manage.xym.com/rest/content/category post 
	 * @param pid 父id
	 * @param name  新建的内容节点
	 * @return ContentCategory  ID
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<ContentCategory> insertContentCategory(ContentCategory contentCategory) {

		try {
			if(contentCategory.getName()==null){
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			
			Boolean flag =contentCategoryService.saveContentCategory(contentCategory);
			if(flag){
				return ResponseEntity.status(HttpStatus.CREATED).body(contentCategory);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * manage.xym.com/rest/content/category post 
	 * @param id  
	 * @param name  重命名
	 * @return void
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> updateContentCategory(ContentCategory contentCategory) {

		try {
			if(contentCategory.getName()==null){
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			
			   Integer i = contentCategoryService.updateSelective(contentCategory);
			   if(i>0){
				   return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			   }
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	

	/**
	 * 异步请求 加载内容分类 返回json数据
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<List<ContentCategory>> queryContentCategory(
			@RequestParam(value = "id", defaultValue = "0") Long pid) {

		try {
			ContentCategory contentCategory = new ContentCategory();
			contentCategory.setParentId(pid);
			
			List<ContentCategory> list = contentCategoryService.queryListByWhere(contentCategory);
			if (CollectionUtils.isEmpty(list)) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.status(HttpStatus.OK).body(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
				null);
	}
}
